<template>
  <div class="app-container">
    <div class="" style="width:100%;padding:10px;height:50px">
      <el-button size="mini" type="primary" style="float:left" @click="handleCreate()">新增</el-button>
      <el-upload
        class="upload-demo"
        :action="getUpload()"
        :headers="getHeaders()"
        :file-list="fileList"
        :on-success="setPage"
        style="float:left"
        multiple>
        <el-button size="mini" type="primary" style="float:left;margin-left: 10px;">点击上传</el-button>
        <div class="el-upload__tip" slot="tip" style="float:right;margin-left: 10px;margin-right: 10px;">只能上传xlsx文件，且不超过5MB</div>
      </el-upload>
      <!--<el-button size="mini" type="primary" style="float:left" @click="handleCreate()">Excel导入</el-button>-->
      <el-button size="mini" type="primary" style="float:left" @click="handleDownload()">导入模板下载</el-button>
      <el-button
        size="mini"
        type="primary"
        style="float: right; margin-left: 10px"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索
      </el-button>
      <el-input
            v-model="listQuery.teacherName_LIKE"
            autocomplete="off"
            placeholder="请输入姓名"
        style="float: right; width: 150px;margin-left: 5px;"
          />
          <el-input
            v-model="listQuery.teacherNum_LIKE"
            autocomplete="off"
            placeholder="请输入工号"
        style="float: right; width: 150px;margin-left: 5px;"
          />
          <el-input
            v-model="listQuery.college_LIKE"
            autocomplete="off"
            placeholder="请输入学院"
        style="float: right; width: 150px;margin-left: 5px;"
          />
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :header-cell-style="{'background-color': '#F5F8FB'}"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="序号" type="index" :index="indexMethod" />
      <!--<el-table-column align="center" label="企业ID">
        <template slot-scope="scope">
          <span :title="scope.row.businessId">{{ scope.row.businessId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="">
        <template slot-scope="scope">
          <span :title="scope.row.createId">{{ scope.row.createId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="">
        <template slot-scope="scope">
          <span :title="scope.row.createTime">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="">
        <template slot-scope="scope">
          <span :title="scope.row.delFlg">{{ scope.row.delFlg }}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="教师ID">
        <template slot-scope="scope">
          <span :title="scope.row.teacherId">{{ scope.row.teacherId }}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="教师姓名">
        <template slot-scope="scope">
          <span :title="scope.row.teacherName">{{ scope.row.teacherName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="教师号">
        <template slot-scope="scope">
          <span :title="scope.row.teacherNum">{{ scope.row.teacherNum }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系方式">
        <template slot-scope="scope">
          <span :title="scope.row.telephone">{{ scope.row.telephone }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="学院">
        <template slot-scope="scope">
          <span :title="scope.row.introduce">{{ scope.row.college }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所在系">
        <template slot-scope="scope">
          <span :title="scope.row.introduce">{{ scope.row.department }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属专业">
        <template slot-scope="scope">
          <span :title="scope.row.introduce">{{ scope.row.major }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="教师介绍">
        <template slot-scope="scope">
          <span :title="scope.row.introduce">{{ scope.row.introduce }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="">
        <template slot-scope="scope">
          <span :title="scope.row.updateId">{{ scope.row.updateId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="">
        <template slot-scope="scope">
          <span :title="scope.row.updateTime">{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>-->
      <el-table-column align="center" label="是否已绑定">
        <template slot-scope="scope">
          <span :title="scope.row.userId?'已绑定':'未绑定'">{{ scope.row.userId?'已绑定':'未绑定' }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="teacher" :visible.sync="dialogVisible">
      <el-form ref="teacherForm" :model="teacher" :rules="rules" size="mini">
        <!--<el-form-item label="企业ID" prop="businessId" :label-width="formLabelWidth">
          <el-input v-model="teacher.businessId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="" prop="createId" :label-width="formLabelWidth">
          <el-input v-model="teacher.createId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="" prop="createTime" :label-width="formLabelWidth">
          <el-input v-model="teacher.createTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="" prop="delFlg" :label-width="formLabelWidth">
          <el-input v-model="teacher.delFlg" autocomplete="off" />
        </el-form-item>
        
        <el-form-item label="教师ID" prop="teacherId" :label-width="formLabelWidth">
          <el-input v-model="teacher.teacherId" autocomplete="off" />
        </el-form-item>-->
        <el-form-item label="教师号" prop="teacherNum" :label-width="formLabelWidth">
          <el-input v-model="teacher.teacherNum" autocomplete="off" />
        </el-form-item>
        <el-form-item label="教师姓名" prop="teacherName" :label-width="formLabelWidth">
          <el-input v-model="teacher.teacherName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="联系方式" prop="telephone" :label-width="formLabelWidth">
          <el-input v-model="teacher.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学院" prop="college" :label-width="formLabelWidth">
          <el-input v-model="teacher.college" autocomplete="off" />
        </el-form-item>
        <el-form-item label="院系" prop="department" :label-width="formLabelWidth">
          <el-input v-model="teacher.department" autocomplete="off" />
        </el-form-item>
        <el-form-item label="专业" prop="major" :label-width="formLabelWidth">
          <el-input v-model="teacher.major" autocomplete="off" />
        </el-form-item>
        <el-form-item label="教师介绍" prop="introduce" :label-width="formLabelWidth">
          <el-input v-model="teacher.introduce" autocomplete="off" />
        </el-form-item>
        <!--<el-form-item label="" prop="updateId" :label-width="formLabelWidth">
          <el-input v-model="teacher.updateId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="" prop="updateTime" :label-width="formLabelWidth">
          <el-input v-model="teacher.updateTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId" :label-width="formLabelWidth">
          <el-input v-model="teacher.userId" autocomplete="off" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { del, get, getPage, save, update ,getUploadUrl} from '@/api/Teacher'

import { getDownloadUrl } from '@/api/Common'

export default {
  data() {
    return {
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        size: 10
      },
      teacher: {
        businessId: null,
        createId: null,
        createTime: null,
        delFlg: null,
        introduce: null,
        teacherId: null,
        teacherName: null,
        teacherNum: null,
        telephone: null,
        updateId: null,
        updateTime: null,
        userId: null,
      },
      teacherCopy: null,
      rules: {
        businessId: [{ required: true, message: '请填写企业ID', trigger: 'blur' }],
        createId: [{ required: true, message: '请填写', trigger: 'blur' }],
        createTime: [{ required: true, message: '请填写', trigger: 'blur' }],
        delFlg: [{ required: true, message: '请填写', trigger: 'blur' }],
        introduce: [{ required: true, message: '请填写教师介绍', trigger: 'blur' }],
        teacherId: [{ required: true, message: '请填写教师ID', trigger: 'blur' }],
        teacherName: [{ required: true, message: '请填写教师姓名', trigger: 'blur' }],
        teacherNum: [{ required: true, message: '请填写教师号', trigger: 'blur' }],
        telephone: [{ required: true, message: '请填写联系方式', trigger: 'blur' }],
        updateId: [{ required: true, message: '请填写', trigger: 'blur' }],
        updateTime: [{ required: true, message: '请填写', trigger: 'blur' }],
        userId: [{ required: true, message: '请填写用户ID', trigger: 'blur' }],
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getHeaders(){
      var headers = {'Authorization':localStorage.getItem('access_token')};
      return headers;
    },
    getUpload(){
      return getUploadUrl();
    },
    setPage(res,file){
      if(res.code===200){
        this.getPage();
        this.fileList = []
      }else{
        this.$message({
          type: 'danger',
          message: '上传失败，'+res.msg
        })
      }
    },
    handleDownload(){
      getDownloadUrl({fileName: "teacherImport.xlsx"}).then(response => {
        let url = window.URL.createObjectURL(new Blob([response]));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', "teacherImport.xlsx");
        document.body.appendChild(link);
        link.click();
      })
    },
    getPage() {
      this.listLoading = true
      getPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getPage()
    },
    indexMethod(index) {
      return this.listQuery.size * (this.listQuery.page - 1) + index + 1
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },
    handleCreate() {
      Object.keys(this.teacher).forEach(key => (this.teacher[key] = null))
      this.teacherCopy = null
      this.dialogVisible = true
      if (this.$refs['teacherForm'] !== undefined) {
        this.$refs['teacherForm'].clearValidate()
      }
    },
    handleEdit(row) {
      const params = { id: row.teacherId }
      get(params).then(response => {
        this.teacher = response.data
        this.teacherCopy = response.data
      })
      this.dialogVisible = true
      if (this.$refs['teacherForm'] !== undefined) {
        this.$refs['teacherForm'].clearValidate()
      }
    },
    handleDelete(row) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.teacherId).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    handleSaveOrUpdate() {
      this.$refs['teacherForm'].validate((valid) => {
        if (valid) {
          if (this.teacher.teacherId) {
            update(this.teacher).then(() => {
              this.$message({
                type: 'success',
                message: '更新成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          } else {
            save(this.teacher).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          }
        } else {
          this.$message.error('请保证数据填写正确！')
        }
      })
    }
  }
}
</script>

<style>
.el-input__icon {
  line-height: 30px;
}
.el-input__inner {
  height: 30px;
  line-height: 30px;
}
</style>
